package binary_tree.Leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 借助队列判断是否为平衡树
 */
public class Num101_SymmetricTreeNoRecursion {
    public boolean isSymmetric(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        //将左右子树入队
        queue.offer(root.left);
        queue.offer(root.right);
        while (!queue.isEmpty()){
            //进行出队操作  比较出队元素
            TreeNode t1=queue.poll();
            TreeNode t2=queue.poll();
            if (t1==null&&t2==null){
                //左右两棵树都是空继续判断
                continue;
            }
            if (t1==null||t2==null){
                return false;
            }
            if (t1.val!=t2.val){
                return false;
            }
            //t1=t2 比较t1.left==t2.right t1.right==t2.left
            queue.offer(t1.left);
            queue.offer(t2.right);
            queue.offer(t1.right);
            queue.offer(t2.left);
        }
        return true;
        }

    }
